package com.ptt.sqm.activity;

import java.net.UnknownHostException;
import java.util.Map;

import android.app.Activity;
import android.app.FragmentManager;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.ptt.sqm.model.Login;
import com.ptt.sqm.model.PendingSummaryList;
import com.ptt.sqm.util.AppUtil;
import com.ptt.sqm.util.WidgetUtil;
import com.ptt.sqm.ws.AsyncResponse;
import com.ptt.sqm.ws.DefaultTask;
import com.ptt.sqmonmobile.R;

/**
 * A placeholder fragment containing a simple view.
 */
public class HomeSummaryAssessFragment extends DefaultFragment implements AsyncResponse {
	/**
	 * The fragment argument representing the section number for this fragment.
	 */
	private static final String ARG_SECTION_NUMBER = "section_number";

	// Create Webservice class object
	private DefaultTask mTask = null;

	private PendingSummaryList pending;

	private static HomeSummaryAssessFragment fragment;

	private View mProgressSearchView;

	private View mContentSearchView;

	/**
	 * Returns a new instance of this fragment for the given section number.
	 */
	public static HomeSummaryAssessFragment newInstance(int sectionNumber) {
		fragment = new HomeSummaryAssessFragment();
		Bundle args = new Bundle();
		args.putInt(ARG_SECTION_NUMBER, sectionNumber);
		fragment.setArguments(args);

		return fragment;
	}

	public HomeSummaryAssessFragment() {
	}

	public void onViewCreated(View view, Bundle savedInstanceState) {
		super.onViewCreated(view, savedInstanceState);

		// Loading...
		mProgressSearchView = view.findViewById(R.id.search_listview_progress);
		mContentSearchView = view.findViewById(R.id.search_listview);

		Login login = session.getUserDetails();

		TextView fullnameAssess = (TextView) view.findViewById(R.id.fullname_assess);
		TextView groupAssess = (TextView) view.findViewById(R.id.group_assess);

		fullnameAssess.setText(login.User_Name);
		groupAssess.setText(login.User_Group_Name);

		if (!login.isAdmin()) {
			WidgetUtil.showProgress(mContentSearchView, mProgressSearchView, true);

			mTask = new DefaultTask(com, getString(R.string.method_searchpendingsummary));
			mTask.delegate = fragment;
			mTask.execute();
		}

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.home_summary_assess, container, false);
		return rootView;
	}

	private class adtSearchMultipleLine extends BaseAdapter {
		private class ListViewCustomItem {
			public TextView year;
			public TextView project;
			public TextView sumPending;
			public TextView sumDraft;
			public TextView sumSubmit;
			public TextView sumTotal;
		}

		@Override
		public int getCount() {
			// TODO Auto-generated method stub
			return pending.PendingSummary.size();
		}

		@Override
		public Object getItem(int arg0) {
			// TODO Auto-generated method stub
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			// TODO Auto-generated method stub
			return 0;
		}

		@Override
		public View getView(final int arg0, View arg1, ViewGroup arg2) {
			// TODO Auto-generated method stub
			arg1 = LayoutInflater.from(HomeSummaryAssessFragment.this.getActivity()).inflate(R.layout.searchsummary_listitem, null);

			ListViewCustomItem lsvItem = new ListViewCustomItem();
			lsvItem.year = (TextView) arg1.findViewById(R.id.year_assess);
			lsvItem.project = (TextView) arg1.findViewById(R.id.project_assess);
			lsvItem.sumPending = (TextView) arg1.findViewById(R.id.sum_pending);
			lsvItem.sumDraft = (TextView) arg1.findViewById(R.id.sum_draft);
			lsvItem.sumSubmit = (TextView) arg1.findViewById(R.id.sum_submit);
			lsvItem.sumTotal = (TextView) arg1.findViewById(R.id.sum_status);

			if (pending.PendingSummary.get(arg0).YearName != null) {
				lsvItem.year.setText(pending.PendingSummary.get(arg0).YearName);
			}

			if (pending.PendingSummary.get(arg0).ProjectName != null) {
				lsvItem.project.setText(pending.PendingSummary.get(arg0).ProjectName);
			}

			lsvItem.sumPending.setText(String.valueOf(pending.PendingSummary.get(arg0).Pending));
			lsvItem.sumDraft.setText(String.valueOf(pending.PendingSummary.get(arg0).Draft));
			lsvItem.sumSubmit.setText(String.valueOf(pending.PendingSummary.get(arg0).Submit));
			lsvItem.sumTotal.setText(String.valueOf(pending.PendingSummary.get(arg0).Total));

			// Go to HomeAssess
			arg1.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View view) {
					FragmentManager fragmentManager = getFragmentManager();
					fragmentManager.beginTransaction().setCustomAnimations(R.anim.enter, R.anim.exit, R.anim.pop_enter, R.anim.pop_exit).addToBackStack(AppUtil.STACK_HOME_SUMMARY_ASSESS)
						.replace(R.id.container, HomeAssessFragment.newInstance(AppUtil.NAV_HOME_ASSESS, pending.PendingSummary.get(arg0))).commit();

				}
			});

			return arg1;
		}

		@Override
		public void unregisterDataSetObserver(DataSetObserver observer) {
			if (observer != null) {
				super.unregisterDataSetObserver(observer);
			}
		}
	}

	@Override
	public void processFinish(String method, Object output) {
		// TODO Auto-generated method stub
		try {
			if (method.equals(getString(R.string.method_searchpendingsummary)))
				getSearchPendingSummary(method, output);

		} catch (Exception e) {
		}
	}

	private void getSearchPendingSummary(String method, Object output) {
		// TODO Auto-generated method stub
		try {
			pending = (PendingSummaryList) gson.fromJson((String) output, PendingSummaryList.class);
			ListView listSearch = (ListView) getView().findViewById(R.id.search_listview);
			if (pending != null && pending.PendingSummary.size() != 0) {
				adtSearchMultipleLine adt = new adtSearchMultipleLine();
				listSearch.setAdapter(adt);

				Log.i(method, "Call WS Success");

			} else {
				Log.i(method, "Call WS Failed");
			}
		} catch (Exception e) {
			Log.i(method, "Call WS Failed");
		}

		WidgetUtil.showProgress(mContentSearchView, mProgressSearchView, false);
	}

	@Override
	public void processFinish(String method, Object output, Object... objects) {
		// TODO Auto-generated method stub

	}

}
